/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pb138.videodes.desktop;

import cz.muni.fi.pb138.videodes.entity.Movie;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author vaclav
 */
public class MovieTableModel extends AbstractTableModel {

    private List<Movie> movies;
    
    public MovieTableModel() {
        movies = new ArrayList<Movie>();
    }
    
    public MovieTableModel(List<Movie> movies) {
        this.movies = movies;
        
        int lastRow = this.movies.size() - 1;
        fireTableRowsInserted(lastRow, lastRow);
    }
    
    public void add(Movie movie) {
        movies.add(movie);
        
        int lastRow = movies.size() - 1;
        fireTableRowsInserted(lastRow, lastRow);
    }
    
    public void remove(Movie movie) {
        movies.remove(movie);
        
        int lastRow = movies.size() - 1;
        fireTableRowsDeleted(lastRow, lastRow);
    }
    
    public void update (Movie movie) {
        for (Movie m : movies) {
            if (m.equals(movie)) {
                m.setName(movie.getName());
                m.setDirector(movie.getDirector());
                m.setYear(movie.getYear());
                m.setCountry(movie.getCountry());
                m.setActors(movie.getActors());
                m.setGenres(movie.getGenres());
                int lastRow = movies.size() - 1;
                
                fireTableRowsUpdated(lastRow, lastRow);
                return;
            }
        }
    }
    
    public List<Movie> getAllMovies() {
        return Collections.unmodifiableList(movies);
    }

    @Override
    public int getRowCount() {
        return movies.size();
    }

    @Override
    public int getColumnCount() {
        return 6;
    }

    @Override
    public Object getValueAt(int r, int c) {
        Movie movie = movies.get(r);        
        switch (c) {
            case 0:
                return movie.getName();
            case 1:
                return movie.getDirector();
            case 2:
                return movie.getYear();
            case 3:
                return movie.getCountry();
            case 4:
                StringBuilder actors = new StringBuilder(movie.getActors().toString());
                actors.deleteCharAt(0);
                actors.deleteCharAt(actors.lastIndexOf("]"));
                return actors.toString();
            case 5: 
                StringBuilder genres = new StringBuilder(movie.getGenres().toString());
                genres.deleteCharAt(0);
                genres.deleteCharAt(genres.lastIndexOf("]"));
                return genres.toString();
            case -1:
                return movie.getId();
            case -2:
                return movie;
            default:
                throw new IllegalArgumentException("Invalid column index: " + c);
        }
    }
    
    @Override
    public String getColumnName(int c) {
        switch (c) {
            case 0:
                return "Name";
            case 1:
                return "Director";
            case 2:
                return "Year";
            case 3:
                return "Country";
            case 4:
                return "Actors";
            case 5:
                return "Genres";
            default:
                throw new IllegalArgumentException("Invalid column index: " + c);
        }
    }
    
    @Override
    public Class<?> getColumnClass(int c) {
        switch (c) {
            case 0:
            case 1:
            case 3:
            case 4:
            case 5:
                return String.class;
            case 2:
                return Integer.class;
            default:
                throw new IllegalArgumentException("Invalid column index: " + c);
        }
    }
    
}
